/*
 * @Descripttion: 
 * @Author: ziwei
 * @Date: 2022-07-07 14:36:17
 * @LastEditors: ziwei
 * @LastEditTime: 2022-07-07 15:15:36
 */
export {}
//1 Proxy 代理
type Proxy<T>={
    get():T,
    set(value:T):void;
}
type Proxify<T>={
    [P in keyof T]:Proxy<T[P]>
}
function proxify<T>(obj:T):Proxify<T>{
    let result = <Proxify<T>>{}
    for(const key in obj){
        Object.defineProperty(result,key,{
            get:()=>{
                console.log('get',key);
                return obj[key]
            },
            set:(value)=>{
                console.log('set',key,value);
                obj[key] = value
            }
        })
    }
    // for(const key in obj){
    //     result[key] = {
    //         get:()=>obj[key],
    //         set:(value)=>obj[key]=value
    //     }
    // }
    return result
}
interface Props{
    name:string
    age:number
}
//proxy 代理
let props:Props ={
    name:"xx",
    age:10
}
let proxyProps:any = proxify<Props>(props)
//console.log(proxyProps)
console.log(proxyProps.name)
proxyProps.name ='xx'
proxyProps.age =10
console.log(proxyProps.name);

//取消代理
function unProxify<T>(t:Proxify<T>):T{
    let result:any = {} as T
    for(const key in t){
        result[key] = t[key];
    }
    return result
}
let originalProps = unProxify<Props>(proxyProps)
console.log(originalProps);
